|
|
|
A running program is a pool
of objects where objects are created, destroyed and interacting. This
interacting is based on messages which are sent from one object to
another asking the recipient to apply a method on itself. To give you an
understanding of this communication, let's come back to the class Integer
presented earlier. In our pseudo programming language we could create new
objects and invoke methods on them. For example, we could use
|
|
Integer i; /*
Define a new integer object */
|
|
i.setValue(1);
/* Set its value to 1 */
|
|
to express the fact, that
the integer object i should set its value to 1. This is the message
``Apply method setValue with argument 1 on yourself.'' sent to object i.
We notate the sending of a message with ``.''. This notation is also used in
C++; other object-oriented languages might use other notations, for example
“- ''.
|
|
Sending a message asking an
object to apply a method is similar to a procedure call in “traditional''
programming languages. However, in object-orientation there is a view of
autonomous objects which communicate with each other by exchanging messages.
Objects react when they receive messages by applying methods on themselves.
They also may deny the execution of a method, for example if the calling
object is not allowed to execute the requested method.
|
|
In our example, the message
and the method which should be applied once the message is received have the
same name: We send “setValue with argument 1'' to object i which
applies “setValue(1)''.
|
|
Consequently, invocation of
a method is just a reaction caused by receipt of a message. This is only
possible, if the method is actually known to the object.
|